package com.hudongwx.food.fargment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hudongwx.food.R;
import com.hudongwx.food.utils.MyJsonObject;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.core.BitmapSize;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * 饮食养生
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_health)
public class HealthFragment extends Fragment {
    private View view;
    private MyHealthAdapter myHealthAdapter;
    @ViewInject(R.id.health_recyclerView)
    private RecyclerView recyclerView;
    private JSONArray jsonArray;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null==view){
            view = x.view().inject(this,inflater,container);
            GridLayoutManager manager = new GridLayoutManager(getActivity(),1);
            recyclerView.setLayoutManager(manager);
            myHealthAdapter = new MyHealthAdapter(jsonArray);
            recyclerView.setAdapter(myHealthAdapter);
            getitem();
        }
        return view;
    }
    class MyHealthAdapter extends RecyclerView.Adapter<MyHealthViewHolder>{
        JSONArray array;
        public MyHealthAdapter(JSONArray array) {
            this.array = array;
        }
        public void getItem(JSONArray array){
            this.array = array;
            notifyDataSetChanged();
        }

        @Override
        public MyHealthViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View viewhealth = LayoutInflater.from(getActivity()).inflate(R.layout.health_list, null);
            return new MyHealthViewHolder(viewhealth);
        }

        @Override
        public void onBindViewHolder(MyHealthViewHolder holder, int position) {
            if (null==array){
                recyclerView.setVisibility(View.GONE);
            }else {
                recyclerView.setVisibility(View.VISIBLE);
                try {
                    x.image().bind(holder.imageView,array.getJSONObject(position).getString("UserIcon"));
                    holder.tv1.setText(array.getJSONObject(position).getString("UserName"));
                    holder.tv2.setText(array.getJSONObject(position).getString("DateTime"));
                    holder.tv3.setText(array.getJSONObject(position).getString("Title"));
                    holder.tv4.setText(array.getJSONObject(position).getString("Content"));
                    JSONArray array1 = array.getJSONObject(position).getJSONArray("ListFile");
                    BitmapUtils utils = new BitmapUtils(getActivity());
                    BitmapDisplayConfig config = new BitmapDisplayConfig();
                    if (array1.length()==1){
                        String url = array1.getJSONObject(0).getString("Url");
                        utils.display(holder.iv1,url,config);
                    }

                    else if (array1.length()==2){
                        String url = array1.getJSONObject(0).getString("Url");
                        String url1 = array1.getJSONObject(1).getString("Url");
                        utils.display(holder.iv1,url,config);
                        utils.display(holder.iv2,url1,config);
                    }else if (array1.length()>=3){
                        String url = array1.getJSONObject(0).getString("Url");
                        String url1 = array1.getJSONObject(1).getString("Url");
                        String url2 = array1.getJSONObject(2).getString("Url");
                        utils.display(holder.iv1,url,config);
                        utils.display(holder.iv2,url1,config);
                        utils.display(holder.iv3,url2,config);
                    }else if (array1.length()==0){
                        holder.layout.setVisibility(View.GONE);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public int getItemCount() {
            if (null==array)return 0;
            return array.length();
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }
    }
    class MyHealthViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView tv1,tv2,tv3,tv4;
        LinearLayout layout;
        private ImageView iv1,iv2,iv3;
        public MyHealthViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.health_img);
            tv1 = (TextView) itemView.findViewById(R.id.health_tv1);
            tv2 = (TextView) itemView.findViewById(R.id.health_tv2);
            tv3 = (TextView) itemView.findViewById(R.id.health_tv3);
            tv4 = (TextView) itemView.findViewById(R.id.health_tv4);
            layout = (LinearLayout) itemView.findViewById(R.id.health_linearLayout);
            iv1 = (ImageView) itemView.findViewById(R.id.heimg_1);
            iv2 = (ImageView) itemView.findViewById(R.id.heimg_2);
            iv3= (ImageView) itemView.findViewById(R.id.heimg_3);
        }
    }
    public void getitem() {

        RequestParams params = new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/Posts?boardId=2&pageSize=5&pageIndex=1");
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    myHealthAdapter.getItem(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
